﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;
using System.Data.SqlClient;

namespace EnterpriseFund.DAL
{
    /// <summary>
    /// 数据访问类:EnterpriseProp
    /// </summary>
    public partial class EnterpriseProp
    {
        public EnterpriseProp()
        { }
        #region  BasicMethod

        /// <summary> 
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string FEnterprisePropNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from fund.t_EnterpriseProp");
            strSql.Append(" where FEnterprisePropNo=@FEnterprisePropNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEnterprisePropNo", SqlDbType.NVarChar,6)	};
            parameters[0].Value = FEnterprisePropNo;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        public string GetFEnterprisePropNoByFEnterprisePropName(string FEnterprisePropName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT TOP 1 FEnterprisePropNo FROM fund.t_EnterpriseProp ");
            strSql.Append("where  FEnterprisePropName=@FEnterprisePropName ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEnterprisePropName", SqlDbType.NVarChar,20)	};
            parameters[0].Value = FEnterprisePropName;
              object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
            if (obj == null)
            {
                return "";
            }
            else
            {
                return obj.ToString();
            }
           
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void UpdateEnterpriseFinanceProp(EnterpriseFund.Model.EnterpriseProp model, List<Model.EnterpriseFinanceProp> enterpriseFinancePropList)
        {
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            List<CommandInfo> cmdList = new List<CommandInfo>();

            strSql.Append("delete from fund.t_EnterpriseFinanceProp where ");
            strSql.Append("FEnterprisePropNo = @FEnterprisePropNo and FYear=@FYear");
            SqlParameter[] parameters = {
					new SqlParameter("@FEnterprisePropNo", SqlDbType.NVarChar,6),
                                        	new SqlParameter("@FYear", SqlDbType.Decimal,5)};
            parameters[0].Value = model.FEnterprisePropNo;
            parameters[1].Value = model.FYear;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            foreach (Model.EnterpriseFinanceProp eModel in enterpriseFinancePropList)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("insert into fund.t_EnterpriseFinanceProp(");
                strSql.Append("FFinancePropNo,FYear,FEnterprisePropNo)");
                strSql.Append(" values (");
                strSql.Append("@FFinancePropNo,@FYear,@FEnterprisePropNo)");
                SqlParameter[] parameters2 = {
					new SqlParameter("@FFinancePropNo", SqlDbType.NVarChar,6),
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FEnterprisePropNo", SqlDbType.NVarChar,6)};
                parameters2[0].Value = eModel.FFinancePropNo;
                parameters2[1].Value = eModel.FYear;
                parameters2[2].Value = eModel.FEnterprisePropNo;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);
            }

            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList);
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(EnterpriseFund.Model.EnterpriseProp model, List<Model.EnterpriseFinanceProp> enterpriseFinancePropList)
        {
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            List<CommandInfo> cmdList = new List<CommandInfo>();
            strSql.Append("insert into fund.t_EnterpriseProp(");
            strSql.Append("FEnterprisePropNo,FEnterprisePropName,fIsByTaxFinanceProp)");
            strSql.Append(" values (");
            strSql.Append("@FEnterprisePropNo,@FEnterprisePropName,@fIsByTaxFinanceProp)");
            SqlParameter[] parameters = {
					new SqlParameter("@FEnterprisePropNo", SqlDbType.NVarChar,6),
					new SqlParameter("@FEnterprisePropName", SqlDbType.NVarChar,30),
					new SqlParameter("@fIsByTaxFinanceProp", SqlDbType.NVarChar,1)};
            parameters[0].Value = model.FEnterprisePropNo;
            parameters[1].Value = model.FEnterprisePropName;
            parameters[2].Value = model.FIsByTaxFinanceProp;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            foreach (Model.EnterpriseFinanceProp eModel in enterpriseFinancePropList)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("insert into fund.t_EnterpriseFinanceProp(");
                strSql.Append("FFinancePropNo,FYear,FEnterprisePropNo)");
                strSql.Append(" values (");
                strSql.Append("@FFinancePropNo,@FYear,@FEnterprisePropNo)");
                SqlParameter[] parameters2 = {
					new SqlParameter("@FFinancePropNo", SqlDbType.NVarChar,6),
					new SqlParameter("@FYear", SqlDbType.Decimal,5),
					new SqlParameter("@FEnterprisePropNo", SqlDbType.NVarChar,6)};
                parameters2[0].Value = eModel.FFinancePropNo;
                parameters2[1].Value = eModel.FYear;
                parameters2[2].Value = eModel.FEnterprisePropNo;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);
            }

            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(EnterpriseFund.Model.EnterpriseProp model, List<Model.EnterpriseFinanceProp> enterpriseFinancePropList)
        {
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            List<CommandInfo> cmdList = new List<CommandInfo>();
            strSql.Append("update fund.t_EnterpriseProp set ");
            strSql.Append("FEnterprisePropName=@FEnterprisePropName,");
            strSql.Append("FIsByTaxFinanceProp=@fIsByTaxFinanceProp");
            strSql.Append(" where FEnterprisePropNo=@FEnterprisePropNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEnterprisePropName", SqlDbType.NVarChar,30),
                    new SqlParameter("@fIsByTaxFinanceProp", SqlDbType.NVarChar,1),
					new SqlParameter("@FEnterprisePropNo", SqlDbType.NVarChar,6)};
            parameters[0].Value = model.FEnterprisePropName;
            parameters[1].Value = model.FIsByTaxFinanceProp;
            parameters[2].Value = model.FEnterprisePropNo;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList);
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string FEnterprisePropNo)
        {
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            List<CommandInfo> cmdList = new List<CommandInfo>();
            strSql.Append("delete from fund.t_EnterpriseFinanceProp ");
            strSql.Append(" where  FEnterprisePropNo=@FEnterprisePropNo ");
            SqlParameter[] parameters3 = {
					new SqlParameter("@FEnterprisePropNo", SqlDbType.NVarChar,6)			};
            parameters3[0].Value = FEnterprisePropNo;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters3;
            cmdList.Add(cmds);

            cmds = new CommandInfo();
            strSql = new StringBuilder();
            strSql.Append("delete from fund.t_EnterpriseProp ");
            strSql.Append(" where FEnterprisePropNo=@FEnterprisePropNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEnterprisePropNo", SqlDbType.NVarChar,6),
					new SqlParameter("@FYear", SqlDbType.Decimal,5)			};
            parameters[0].Value = FEnterprisePropNo;

            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList);
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public EnterpriseFund.Model.EnterpriseProp GetModel(string FEnterprisePropNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FEnterprisePropNo,FEnterprisePropName,FIsByTaxFinanceProp from fund.t_EnterpriseProp ");
            strSql.Append(" where FEnterprisePropNo=@FEnterprisePropNo ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEnterprisePropNo", SqlDbType.NVarChar,6)		};
            parameters[0].Value = FEnterprisePropNo;

            EnterpriseFund.Model.EnterpriseProp model = new EnterpriseFund.Model.EnterpriseProp();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public EnterpriseFund.Model.EnterpriseProp DataRowToModel(DataRow row)
        {
            EnterpriseFund.Model.EnterpriseProp model = new EnterpriseFund.Model.EnterpriseProp();
            if (row != null)
            {
                if (row["FEnterprisePropNo"] != null)
                {
                    model.FEnterprisePropNo = row["FEnterprisePropNo"].ToString();
                }
                if (row["FEnterprisePropName"] != null)
                {
                    model.FEnterprisePropName = row["FEnterprisePropName"].ToString();
                }
                if (row["FIsByTaxFinanceProp"] != null)
                {
                    model.FIsByTaxFinanceProp = row["FIsByTaxFinanceProp"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FEnterprisePropNo,FEnterprisePropName,FIsByTaxFinanceProp ");
            strSql.Append(" FROM fund.t_EnterpriseProp ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" FEnterprisePropNo,FEnterprisePropName ");
            strSql.Append(" FROM fund.t_EnterpriseProp ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM fund.t_EnterpriseProp ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.FYear desc");
            }
            strSql.Append(")AS Row, T.*  from fund.t_EnterpriseProp T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_EnterpriseProp";
            parameters[1].Value = "FYear";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
